using UnityEngine;
using QFramework;
using UnityEngine.Video;
using Unity.Burst.Intrinsics;

namespace ProjectSurvivor
{
	public partial class CarmController : ViewController
	{
		void Start()
		{
			// Code Here
			Application.targetFrameRate = 60;
		}
		private Vector3 mCurrentCamerPos;
		private Vector2 mTartPosition = Vector2.zero;
		private bool mShake = false;

		private int mShakFrame = 0;
		static CarmController mDefault = null;
		private float mShakeA = 0.25f;
		void Awake()
		{
			mDefault = this;
		}
		void OnDestroy()
		{
			mDefault = null;
		}

		public static void Shake()
		{
			mDefault.mShake = true;
			mDefault.mShakFrame = 30;
			mDefault.mShakeA = 0.25f;
		}
		void Update()
		{
			if (Player.Default)
			{
				mTartPosition = Player.Default.transform.position;

				mCurrentCamerPos.x = (1.0f - Mathf.Exp(-Time.deltaTime * 20)).Lerp(transform.position.x, mTartPosition.x);


				mCurrentCamerPos.y = (1.0f - Mathf.Exp(-Time.deltaTime * 5)).Lerp(transform.position.y, mTartPosition.y);

				mCurrentCamerPos.z = transform.position.z;

				if (mShake)
				{
					mShakFrame--;

					var shakeA = Mathf.Lerp(mShakeA, 0.0f, (mShakFrame / 30.0f));

					transform.position = new Vector3(mCurrentCamerPos.x + Random.Range(-shakeA, shakeA), mCurrentCamerPos.y + Random.Range(-shakeA, shakeA), mCurrentCamerPos.z);

					if (mShakFrame <= 0)
					{
						mShake = false;
					}
				}
				else
				{
					transform.PositionX
					((1.0f - Mathf.Exp(-Time.deltaTime * 5)).Lerp(transform.position.x, mTartPosition.x));

					transform.PositionY
					((1.0f - Mathf.Exp(-Time.deltaTime * 5)).Lerp(transform.position.y, mTartPosition.y));
				}

			}

		}
	}
}
